package org.water.sys.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.water.base.entity.R;
import org.water.base.web.BaseController;
import org.water.common.bind.annotation.CurrentUser;
import org.water.common.bind.annotation.MethodDesc;
import org.water.sys.entity.SysResource;
import org.water.sys.entity.SysUser;
import org.water.sys.service.ResourceService;
import org.water.sys.service.RoleResourceService;

/**
 * 资源控制器
 * @author qzy
 *
 */
@Controller
@RequestMapping("/admin/resource")
public class ResourceController extends BaseController {

	@Autowired
	private ResourceService resourceService;
	@Autowired
	private RoleResourceService roleResourceService;
	
	/**
	 * 资源管理主页
	 * @param request
	 * @param response
	 * @return
	 */
	@MethodDesc(name="资源管理",value="查看资源列表")
	@RequestMapping("/index")
	public String index(HttpServletRequest request,HttpServletResponse response)
	{
		return "admin/resource/resouceList";
	}
	/**
	 * 根据父资源查询子资源
	 * @param resourceId
	 * @return
	 */
	
	@RequestMapping("/findByParent")
	public List<SysResource> findByParent(@RequestParam("resourceId") String resourceId)
	{
		return resourceService.findByParent(resourceId);
	}
	/**
	 * 查询可用的资源
	 * @param resourceId
	 * @return
	 */
	
	@RequestMapping("/findUsableResource")
	public SysResource findUsableResource(@RequestParam("resourceId") String resourceId)
	{
		return resourceService.findUsableResource(resourceId);
	}
	/**
	 * 查询单个资源
	 * @param resourceId
	 * @return
	 */
	
	@RequestMapping("/one")
	public SysResource findOne(@RequestParam("resourceId") String resourceId)
	{
		return resourceService.findOne(resourceId);
	}
	/**
	 * 保存
	 * @param sysResource
	 * @return
	 */
	@CacheEvict(value={"userOwnResoucrce","userOwnResoucrceFun"},allEntries=true)
	@MethodDesc(name="资源管理",value="保存资源")
	@RequestMapping("/save")
	public R save(HttpServletRequest request,@CurrentUser SysUser user)
	{
		SysResource sysResource=new SysResource();
		SysResource parent=new SysResource();
		sysResource.setId(request.getParameter("id")!=""?request.getParameter("id"):null);
		sysResource.setName(request.getParameter("name"));
		sysResource.setType(Integer.parseInt(request.getParameter("type")));
		sysResource.setIdentify(request.getParameter("identify"));
		sysResource.setUrl(request.getParameter("url"));
		sysResource.setIcon(request.getParameter("icon"));
		sysResource.setRemark(request.getParameter("remark"));
		sysResource.setApis(request.getParameter("apis"));
		sysResource.setCreateUser(user.getId());
		sysResource.setCreateDate(new Date());
		sysResource.setSnum(Integer.parseInt(request.getParameter("snum")));
		parent.setId(request.getParameter("parent"));
		sysResource.setParent(parent);
		sysResource.setDelStatus(0);
		sysResource.setUseStatus(1);
		resourceService.save(sysResource);
		return getBaseBean(true, "");
	}
	
	/**
	 * 更新
	 * @param sysResource
	 * @return
	 */
	@CacheEvict(value={"userOwnResoucrce","userOwnResoucrceFun"},allEntries=true)
	@MethodDesc(name="资源管理",value="修改资源")
	@RequestMapping("/update")
	public R update(SysResource sysResource)
	{
		resourceService.update(sysResource);
		return getBaseBean(true, "");
	}
	
	/**
	 * 逻辑删除 
	 * @param sysResource
	 * @return
	 */
	@CacheEvict(value={"userOwnResoucrce","userOwnResoucrceFun"},allEntries=true)
	@MethodDesc(name="资源管理",value="删除资源")
	@RequestMapping("/del")
	public R del(@RequestParam("resourceId") String resourceId)
	{
		R bean=getBaseBean(true, "");
		SysResource resource=findOne(resourceId);
		if(roleResourceService.isResourceRelRole(resourceId))
		{
			bean.setSuccess(false);
			bean.setMsg("资源已经被角色关联，不能删除");
		}
		else if(resource!=null)
		{
			resource.setDelStatus(1);
			resourceService.update(resource);
		}
		else
		{
			bean.setSuccess(false);
			bean.setMsg("未找到资源");
		}
		return bean;
	}
	/**
	 * 查询所有资源
	 * @return
	 */
	
	@RequestMapping("/all")
	public List<SysResource> findAll()
	{
		List<SysResource> resources=resourceService.findAllResource();
		return resources;
	}
	/**
	 * 查询用户拥有的资源
	 * @param userId
	 * @return
	 */
	@RequestMapping("/userOwnResource")
	public List<Map<String, Object>> findResourceByUserId(@CurrentUser SysUser user)
	{
		String userId=user.getId();
		List<Map<String, Object>> rs=resourceService.findResourceByUserId(userId);
		return rs;
	}
	/**
	 * 查询用户拥有的菜单功能
	 * @param userId
	 * @return
	 */
	@RequestMapping("/userOwnResourceFun")
	public List<Map<String, Object>> findUserFunByMenu(@CurrentUser SysUser user,@RequestParam("menu") String menu)
	{
		String userId=user.getId();
		List<Map<String, Object>> rs=resourceService.findUserFunByMenu(userId, menu);
		return rs;
	}
}
